package com.vitabuybuy.tv.ui.shopcenter.viewmodel

import androidx.lifecycle.MutableLiveData
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.net.loadingtype.LoadingType
import com.library.common.toast.XToast
import com.vitabuybuy.tv.BR
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.dto.*
import com.vitabuybuy.tv.inter.OnIdItemClickListener
import com.vitabuybuy.tv.network.CartRepository
import com.vitabuybuy.tv.network.ShopCenterRepository
import com.vitabuybuy.tv.ui.tencent.liveroom.repository.LiveRepository
import me.tatarka.bindingcollectionadapter2.ItemBinding
import me.tatarka.bindingcollectionadapter2.OnItemBind

class GoodsDetailsViewModel : BaseViewModel() {
    val liveDetils = MutableLiveData<LiveDteilaDto>()
    val goodDetails = MutableLiveData<GoodsDetailsDto>()
    val descriptionlist = MutableLiveData<MutableList<String>>()

    val recommendlist =
        MutableLiveData<MutableList<GoodsRecommendAndSimilerDto.RecommendProductListBean>>()
    val similerlist =
        MutableLiveData<MutableList<GoodsRecommendAndSimilerDto.SimilarProductListBean>>()

    val itemOnClickListener = object : OnIdItemClickListener {
        override fun onItemClick(goodId: Int) {
            defUI.msgEvent.postValue(MessageModel(3, arg1 = goodId))
        }
    }
    val recommendBinding: OnItemBind<GoodsRecommendAndSimilerDto.RecommendProductListBean> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, item: GoodsRecommendAndSimilerDto.RecommendProductListBean ->
            itemBinding.set(BR.itemBean, R.layout.item_commodity_3).bindExtra(BR.position, position)
                .bindExtra(BR.listenner, itemOnClickListener)
        }

    val similerBinding: OnItemBind<GoodsRecommendAndSimilerDto.SimilarProductListBean> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, item: GoodsRecommendAndSimilerDto.SimilarProductListBean ->
            itemBinding.set(BR.itemBean, R.layout.item_commodity_4).bindExtra(BR.position, position)
                .bindExtra(BR.listenner, itemOnClickListener)
        }

    val descriptionImgBinding: OnItemBind<String> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, item: String ->
            itemBinding.set(BR.itemBean, R.layout.item_description_image)
        }
    fun getDetails(goodId: Int?) {
        rxHttpRequest {
            onRequest = {
                goodDetails.value = ShopCenterRepository.getDetails(goodId).await()
                statusView.set(LoadingType.SHOW_CONTENT)
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = true
        }
    }

    fun addToCart(goodId: Int?, buyNum: Int) {
        rxHttpRequest {
            onRequest = {
                ShopCenterRepository.addToCart(goodId, buyNum).await()
                defUI.msgEvent.postValue(MessageModel(1))
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    fun collectOrcancel(id: Int, isFav: Boolean) {
        rxHttpRequest {
            onRequest = {
                ShopCenterRepository.collectOrcancel(id, isFav).await()
                defUI.msgEvent.postValue(MessageModel(2, obj = isFav))
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    val commentList = MutableLiveData<MutableList<GoodsCommentDto>>()
    fun getGoodsCommentList(id: Int?, isImage: Boolean, pageNumber: Int, pageSize: Int) {
        rxHttpRequest {
            onRequest = {
                commentList.value =
                    ShopCenterRepository.getGoodsCommentList(id, isImage, pageNumber, pageSize)
                        .await()
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }


    fun getGoodsList(goodId: Int?) {
        rxHttpRequest {
            onRequest = {
                val it = ShopCenterRepository.getGoodsRecommendAndSimilerList(goodId).await()
                recommendlist.value = it.recommendProductList
                similerlist.value = it.similarProductList
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    val couponsList = MutableLiveData<MutableList<CouponCenterDto>>()

    fun getStoreCouponsList(storeId: Int?) {
        rxHttpRequest {
            onRequest = {
                couponsList.value = CartRepository.getCouponsList(storeId).await()
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun saveACoupon(couponId: Int, position: Int) {
        rxHttpRequest {
            onRequest = {
                CartRepository.saveACoupon(couponId).await()
                defUI.msgEvent.postValue(MessageModel(4, arg1 = position))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun getLiveDetail(liveId: Int?) {
        rxHttpRequest {
            onRequest = {
                liveDetils.value = LiveRepository.getLiveDetail(liveId).await()
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }
}
